#! /bin/sh

sleep 5

if [ "$DISABLE_IPV6" = "1" ]; then
  export IPV6_LISTEN=""
else
  export IPV6_LISTEN="listen [::]:8080;"
fi

if [ -z "$TERRAT_ACCESS_LOG" ]; then
    export TERRAT_ACCESS_LOG="off"
fi

if [ -z "$TERRATEAM_CLIENT_MAX_BODY_SIZE" ]; then
    export TERRATEAM_CLIENT_MAX_BODY_SIZE="100m"
fi

export TERRATEAM_UI_ANALYTICS=$TERRATEAM_UI_ANALYTICS
export TERRATEAM_UI_SUBSCRIPTION=$TERRATEAM_UI_SUBSCRIPTION
export TERRATEAM_MAINTENANCE=$TERRATEAM_MAINTENANCE
export TERRATEAM_MAINTENANCE_MESSAGE=$TERRATEAM_MAINTENANCE_MESSAGE
export TERRATEAM_UPGRADE_NUDGES=$TERRATEAM_UPGRADE_NUDGES

mkdir -p /run/nginx
envsubst '$TERRAT_UI_BASE $IPV6_LISTEN $TERRAT_ACCESS_LOG $TERRATEAM_CLIENT_MAX_BODY_SIZE $TERRATEAM_UI_ANALYTICS $TERRATEAM_UI_SUBSCRIPTION $TERRATEAM_MAINTENANCE $TERRATEAM_MAINTENANCE_MESSAGE $TERRATEAM_UPGRADE_NUDGES' < /etc/nginx/nginx.conf.template > /etc/nginx/nginx.conf
exec /usr/sbin/nginx -c /etc/nginx/nginx.conf
